﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class pay : System.Web.UI.Page
{
    protected int m_iMemID;
    protected int m_iBasketID;
    protected int m_iTongTien;
    protected void Page_Load(object sender, EventArgs e)
    {
        int iDaLogin = (Int32)Session["IsLogin"];
        if (iDaLogin == 0)
        {
            Response.Redirect("/Common/Login.aspx");
            return;
        }
        int memID;
        if (Request.QueryString["mID"] == null
            || Request.QueryString["bID"] == null
            || Session["UserID"] == null)
        {
            Response.Redirect("/default.aspx");
            return;
        }
        if (!Int32.TryParse(Request.QueryString["mID"], out m_iMemID) || 
            !Int32.TryParse(Request.QueryString["bID"], out m_iBasketID) ||
            !Int32.TryParse(Session["UserID"].ToString(), out memID)
            )
        {
            Response.Redirect("/default.aspx");
            return;
        }

        if (m_iMemID != memID)
        {
            Response.Redirect("/default.aspx");
            return;
        }

        if (!E2_WEBSITE.Basket.GlobalBasket.checkValidBasket(m_iMemID, m_iBasketID))
        {
            Response.Redirect("/default.aspx");
            return;
        }


        if (E2_WEBSITE.Basket.GlobalBasket.checkBasketEmpty(m_iBasketID))
        {
            Response.Redirect("/default.aspx");
            return;
        }
        
        if (!IsPostBack)
        {
            DataBindingPTTT();
            DataBindingPTGH();
            DataBindingBank();
            m_iTongTien = E2_WEBSITE.Basket.GlobalBasket.getTotalPrice(m_iBasketID);
            lbTongTien.Text = E2_WEBSITE.Code.Utilities.ConvertToMoneyFormat(m_iTongTien, true);
        }
    }

    protected void DataBindingBank()
    {
        ddlBank.DataSource = E2_WEBSITE.Basket.GlobalBasket.getBank();
        ddlBank.DataTextField = "TenNganHang";
        ddlBank.DataValueField = "TaiKhoanCongTy";
        ddlBank.DataBind();
    }

    protected void DataBindingPTGH()
    {
        ddlPTGH.DataSource = E2_WEBSITE.Basket.GlobalBasket.getPTGH();
        ddlPTGH.DataTextField = "TenPhuongThuc";
        ddlPTGH.DataValueField = "id";
        ddlPTGH.DataBind();
    }

    protected void DataBindingPTTT()
    {
        ddlPTTT.DataSource = E2_WEBSITE.Basket.GlobalBasket.getPTTT();
        ddlPTTT.DataTextField = "TenPhuongThuc";
        ddlPTTT.DataValueField = "id";
        ddlPTTT.DataBind();
    }

    protected void cvSTK_ServerValidate(object sender, ServerValidateEventArgs args)
    {
        args.IsValid = true;

        if (args.Value.Length != 16)
            args.IsValid = false;
        else
        {
            for (int i = 0; i < args.Value.Length; i++)
            {
                if ((args.Value[i] < '0') || (args.Value[i] > '9'))
                {
                    args.IsValid = false;
                    break;
                }
            }
        }
    }



    protected void cvSAT_ServerValidate(object sender, ServerValidateEventArgs args)
    {
        args.IsValid = true;

        if (args.Value.Length != 3)
            args.IsValid = false;
        else
        {
            for (int i = 0; i < args.Value.Length; i++)
            {
                if ((args.Value[i] < '0') || (args.Value[i] > '9'))
                {
                    args.IsValid = false;
                    break;
                }
            }
        }
    }
    
    protected void cvSDT_ServerValidate(object sender, ServerValidateEventArgs args)
    {
        args.IsValid = true;

        if (args.Value.Length < 8)
            args.IsValid = false;
        else
        {
            for (int i = 0; i < args.Value.Length; i++)
            {
                if ((args.Value[i] < '0') || (args.Value[i] > '9'))
                {
                    args.IsValid = false;
                    break;
                }
            }
        }
    }

    private void runjQueryCode(string jsCodetoRun)
    {

        ScriptManager requestSM = ScriptManager.GetCurrent(this);
        if (requestSM != null && requestSM.IsInAsyncPostBack)
        {
            ScriptManager.RegisterClientScriptBlock(this,
                                                    typeof(Page),
                                                    Guid.NewGuid().ToString(),
                                                    jsCodetoRun,
                                                    true);
        }
        else
        {
            ClientScript.RegisterClientScriptBlock(typeof(Page),
                                                   Guid.NewGuid().ToString(),
                                                   jsCodetoRun,
                                                   true);
        }
    }

    protected void XacNhan_Click(object sender, EventArgs e)
    {
        int iPTGH;
        int iPTTT;
        if (!Int32.TryParse(ddlPTGH.SelectedValue, out iPTGH) ||
            !Int32.TryParse(ddlPTTT.SelectedValue, out iPTTT)
            )
        {
            Response.Redirect("/default.aspx");
            return;
        }
        m_iTongTien = E2_WEBSITE.Basket.GlobalBasket.getTotalPrice(m_iBasketID);
        string sTenNguoiNhan = tbHoTenNguoiNhan.Text;
        string sDiaChiNguoiNhan = tbDiaChiNguoiNhan.Text;
        string sSoDTNguoiNhan = tbSDTNguoiNhan.Text;
        string sSoTaiKhoan = tbSoTaiKhoan.Text;
        string sSoAnToan = tbSoAnToan.Text;
        string sSoTaiKhoanCongTy = ddlBank.SelectedValue;
        string sTenNganHang = ddlBank.SelectedItem.Text;

        // tinh tien KH
        if (sTenNganHang.CompareTo("SEBank") == 0)
        {
            E2_WEBSITE.ServiceReferenceSEBanking.Service1Client sw = new E2_WEBSITE.ServiceReferenceSEBanking.Service1Client();
            int iResult = sw.ExChangeVND(sSoTaiKhoan, sSoAnToan, sSoTaiKhoanCongTy, m_iTongTien);
            int oID = 0;
            if (iResult == 1)
            {
                oID = E2_WEBSITE.Basket.GlobalBasket.payBasket(
                    m_iMemID,
                    m_iBasketID,
                    m_iTongTien,
                    iPTGH,
                    iPTTT,
                    sTenNguoiNhan,
                    sDiaChiNguoiNhan,
                    sSoDTNguoiNhan,
                    sSoTaiKhoanCongTy,
                    sTenNganHang);
            }
            Response.Redirect("/cp/member/ShowOrder.aspx?rsl="+iResult.ToString()+"&oID="+oID.ToString());
        }
    }
}